import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import DefaultLayout from '@/layout/default-layout.vue'
import appRoutes from './routes'
import createRouteGuard from './guard'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const notFoundRoute: RouteRecordRaw = {
  path: '/:path(.*)*',
  name: '404',
  component: () => import('@/views/exception/404.vue'),
  meta: {
    title: '404',
    hideInMenu: true,
    requiresAuth: false
  }
}

export const RootRoute: RouteRecordRaw = {
  path: '/',
  name: 'root',
  component: DefaultLayout,
  redirect: '/device/manage',
  children: [...appRoutes, notFoundRoute]
}

export const LoginRoute: RouteRecordRaw = {
  path: '/login',
  name: 'login',
  component: () => import('@/views/login/index.vue'),
  meta: {
    requiresAuth: false
  }
}

const router = createRouter({
  history: createWebHistory(),
  routes: [RootRoute, LoginRoute],
  scrollBehavior() {
    return { top: 0 }
  }
})

createRouteGuard(router)

export default router
